﻿using Nemerle.Assertions;
using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;
using Nemerle.Compiler;

using System;
using System.Collections.Generic;
using System.Linq;

using N2 = Nemerle2.Compiler;
using PT = Nemerle.Compiler.Parsetree;

namespace Nemerle2.Parser
{
  /// <summary>
  /// Description of AstConverter.
  /// </summary>
  [Record]
  public partial class AstConverter
  {
    manager : ManagerClass;

    [RecordIgnore] mutable typeDecls : List[PT.TopDeclaration];

    [RecordIgnore] mutable locationConverter : LocationConverter;

    public Convert([NotNull]compileUnit : N2.CompileUnit, [NotNull]locationConverter : LocationConverter) : list[PT.TopDeclaration]
    {
      manager.MacroColors.PushUseSiteColor();
      try
      {
        this.typeDecls = List();
        this.locationConverter = locationConverter;

        match(compileUnit)
        {
          | Content(rootNs, _globalAttrs) =>
            ConvertNamespace(manager.CoreEnv, rootNs);

          | Error(msg) as loc => Echo(loc, msg)
        }

        NList.ToList(typeDecls)
      }
      finally
      {
        manager.MacroColors.PopColor();
      }
    }
  }
}
